package com.d0323;

import java.io.*;

public class SerializableTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //ByteArrayOutputStream bos=new ByteArrayOutputStream();//将数据写入到内存中
        //ObjectOutputStream oos=new ObjectOutputStream(bos);
        //Person person=new Person("Jack",22,"大连");
        //oos.writeObject(oos);
        //oos.close();
        //bos.close();
        //
        FileOutputStream fos=new FileOutputStream(new File("src/com/d0323/serializable.txt"));
        ObjectOutputStream oos1=new ObjectOutputStream(fos);//将数据写入到文件中
        Person person1=new Person("Jack",22,"大连");
        oos1.writeObject(person1);
        oos1.close();
        fos.close();

        FileInputStream fis=new FileInputStream(new File("src/com/d0323/serializable.txt"));
        ObjectInputStream ois=new ObjectInputStream(fis);//从流中读取信息
        Person person2=(Person)ois.readObject();
        System.out.println(person2);//返回对象默认调用toString方法
        //person2.toString();
        ois.close();
        fis.close();
    }
}
